<?php
/**
 * @Name: UserSearch.php
 * @Author: yashuai
 */

namespace App\Http\Search\Rbac;

use App\Http\Model\ConstCode;
use App\Http\Model\Org\StaffModel;
use App\Http\Search\BaseSearch;
use App\Http\Model\Rbac\UserModel;
use Illuminate\Support\Facades\DB;

class UserSearch extends BaseSearch
{

    /**
     * @Desc 用户列表搜索
     * @return array
     * @author yashuai
     */
    public function index()
    {
        $where       = [
            'UserStatus' => _array_field($this->params, 'Status', ConstCode::BASIC_STATUS_NORMAL)
        ];
        $select      = ['UserID', 'UserName', 'NickName', 'UserEmail', 'IsSuper', 'UserStatus', 'CreateTime'];
        $this->query = UserModel::with('role')->where($where)->select($select);
        $result      = $this->pageList();
        foreach ($result['list'] as &$val) {
            $val['RoleID'] = array_column($val['role'], 'RoleID');
            unset($val['role']);
        }
        return $result;
    }


    /**
     * @Desc 员工关联账户列表
     * @return array
     * @author yashuai
     */
    public function staffRelation()
    {
        $select      = ['u.UserID', 'u.UserName', 'u.NickName', 'u.UserEmail'];
        $this->query = DB::table(UserModel::tableName().' as u')
            ->leftJoin(StaffModel::tableName().' as s', 'u.UserID', '=', 's.UserID')
            ->where(['u.UserStatus' => ConstCode::BASIC_STATUS_NORMAL])
            ->whereNull('s.StaffID')->select($select);
        return $this->pageList();
    }

}
